<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="object.Message" %>
<%@ page import="Dao.MessageDaoImpt" %>
<%@ page import="jakarta.servlet.http.Cookie" %>
<html>
<head>
    <title>留言删除界面</title>
    <link rel="stylesheet" href="table.css">
    <!--检测是否非法访问-->
    <%
        Cookie cookie[] = request.getCookies();
        String uname = null;
        String uid = null;
        for (int i = 0; i < cookie.length; i++) {
            if (cookie[i].getName().equals("uname")) {
                uname = cookie[i].getValue();
            }
            if (cookie[i].getName().equals("uid")) {
                uid = cookie[i].getValue();
            }
        }
        if (uname == null || uname.equals("")) {
            out.println("<script>alert('非法访问，正在返回登录界面')</script>");
            response.setHeader("refresh", "0;URL=login.jsp");
        }
        if (uid == null || uid.equals("")) {
            out.println("<script>alert('非法访问，正在返回登录界面')</script>");
            response.setHeader("refresh", "0;URL=login.jsp");
        }
    %>
</head>
<body>
<table>
    <tbody>
    <tr>
        <td colspan="7"><h3>即将删除：</h3></td>
    </tr>
    <tr>
        <td>编号</td>
        <td>标题</td>
        <td>用户名</td>
        <td>内容</td>
        <td>是否删除</td>
    </tr>
    <%
        MessageDaoImpt Dao = new MessageDaoImpt();
        String mid = request.getParameter("mid");
        String choose = request.getParameter("choose");
        String type = request.getParameter("type");
        Message message = Dao.SelectMessage(Integer.parseInt(mid));
        //判断是否为本人信息
        if (uname.equals(message.getAuthor())) {
            //新内容为空时说明第一次进入该页面，输出原信息内容
            if (choose == null) {
                //表单
                out.println("<form action=\"\" method=\"post\">\n" +
                        "<tr>\n" + "<td>" + message.getMid() + "</td>\n" +
                        "<td>" + message.getTitle() + "</td>\n" +
                        "<td>" + message.getAuthor() + "</td>\n" +
                        "<td>" + message.getContent() + "</td>\n" +
                        "<td><input type=\"radio\" name=\"choose\" value=\"否\" checked>否<input type=\"radio\" name=\"choose\" value=\"是\">是</td>\n" +
                        "<td><input type=\"submit\" value=\"删除\" name=\"type\"></td>\n" +
                        "<td><input type=\"submit\" value=\"返回\" name=\"type\"></td>\n" +
                        "</tr>\n" + "</form>");
            } else {
                //对比两个message有无变化（信息是否更改），无更改直接返回信息页面
                if (choose.equals("否") || type.equals("返回")) {
                    out.println("<script>alert('正在返回信息界面')</script>");
                } else {
                    int num = Dao.DeleteMessage(message);
                    switch (num) {
                        case -1:
                            out.println("<script>alert('未查找到信息')</script>");
                            break;
                        case -2:
                            out.println("<script>alert('出现问题，请联系管理员')</script>");
                            break;
                        default:
                            out.println("<script>alert('已删除" + num + "条')</script>");
                    }
                }
                response.setHeader("refresh", "0;URL=message.jsp");
            }
        } else {
            out.println("<script>alert('无法删除非本人信息')</script>");
            response.setHeader("refresh", "0;URL=message.jsp");
        }
    %>
    <tr>
    </tr>
    </tbody>
</table>
</body>
</html>
